package com.sofn.rest.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import java.util.Arrays;

@Configuration
public class H2ConsoleConfig implements ServletContextInitializer {

    @Autowired
    private Environment env;

    @Value("${spring.profiles.active}")
    private String profile;

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        System.out.println("env: " + Arrays.asList(env.getActiveProfiles()));
        System.out.println("profile: " + profile);
        ServletRegistration.Dynamic h2ConsoleServlet = servletContext.addServlet("H2Console", new org.h2.server.web.WebServlet());
        h2ConsoleServlet.addMapping("/h2/*");
        h2ConsoleServlet.setLoadOnStartup(1);
    }
}
